package models;

import java.sql.Timestamp;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import play.db.jpa.Model;

@Entity
@Table(name = "sale")
public class Sale extends Model {

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "member_id")
	public Member member;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "product_id")
	public Product product;
	@Column(name = "available")
	public String available;
	@Column(name = "contract")
	public String contract;
	@Column(name = "depict")
	public String depict;
	@Column(name = "email")
	public String email;
	@Column(name = "pic")
	public String pic;
	@Column(name = "price")
	public String price;
	@Column(name = "status")
	public String status;
	@Column(name = "tel")
	public String tel;
	@Column(name = "time", length = 19)
	public Timestamp time;
	@Column(name = "title")
	public String title;

	public Sale() {
	}

	public Sale(Member member, Product product, String available, String contract, String depict, String email,
			String pic, String price, String status, String tel, Timestamp time, String title) {
		this.member = member;
		this.product = product;
		this.available = available;
		this.contract = contract;
		this.depict = depict;
		this.email = email;
		this.pic = pic;
		this.price = price;
		this.status = status;
		this.tel = tel;
		this.time = time;
		this.title = title;
	}
	
	/**
	 * 分页查找特定类型的出售信息
	 * @param page
	 * @param size
	 * @param typeID
	 * @return
	 */
	public static List<Sale> getSales(int page,int size,long typeID){
		
/*		String hql="from Sale sale ,Product product,Types types ,Company company  " +
					"where sale.product=product.id and product.types =types.id and product.company=company.id" +
					" order by sale.time desc ";*/
		
		
		String hql="from Sale sale inner join fetch sale.product " +
					"inner join fetch sale.product.types " +
					"inner join fetch sale.product.company where sale.product.types.id=?";
		
		return find(hql,typeID).from(size*page).fetch(size);
	}

}